package com.putao.spider;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.putao.utils.HttpUtils;

import us.codecraft.webmagic.selector.Html;
import us.codecraft.webmagic.selector.JsonPathSelector;
import us.codecraft.webmagic.utils.UrlUtils;

public class ZhihuTopicQuestionPost {

	public static List<String> post(String topicId,String topicName,String offset) {
		List<String> tempList = new ArrayList<>();
		List<String> questionList = new ArrayList<>();

		Map<String, String> map = new HashMap<>();
		map.put("start", "0");
		while (true) {
			map.put("offset", offset);
			String result = HttpUtils.send("https://www.zhihu.com/topic/" + topicId + "/hot", map, "UTF-8");
			result = UrlUtils.fixAllRelativeHrefs(
					new Html(new JsonPathSelector("$.msg").selectList(result).toString()).toString(),
					"https://www.zhihu.com/");
			// System.out.println(result);
			List<String> temp = new Html(result)
					.xpath("//div[@class=\"feed-item feed-item-hook  folding\"]/@data-score").all();
			List<String> questions = new Html(result)
					.xpath("//div[@class=\"feed-item feed-item-hook  folding\"]/div[@class=\"feed-main\"]/div[@class=\"feed-content\"]/h2/")
					.links().all();
			System.out.println(temp);
			System.out.println(questions);
			if (temp.isEmpty()) {
				break;
			} else {
				offset = temp.get(temp.size() - 1);
				tempList.addAll(temp);
				questionList.addAll(questions);
			}
		}
		System.out.println(tempList);
		System.out.println(questionList);
		return tempList;
	}

	public static void main(String[] args) {
		post("19551556","", "3496.14454604");
	}
}